<template>
  <!-- 组件中只有一个弹窗 -->
  <el-dialog
      v-model="unitAddWinDialogVisible"
      title="添加单位"
      width="50%"
      @close="handleClose"
  >
    <template #default>
      <el-form style="margin-top: 10px" :model="newUnit"  label-width="auto">

        <el-form-item  style="font-weight: bold" label="单位编码">
          <el-input v-model="newUnit.unitCode"/>
        </el-form-item>

        <el-form-item style="font-weight: bold" label="单位名称">
          <el-input v-model="newUnit.unitName"/>
        </el-form-item>

        <el-form-item style="font-weight: bold" label="是否是主单位">
          <el-radio-group v-model="newUnit.primaryFlag">
            <el-radio :label=1>是</el-radio>
            <el-radio :label=2>否</el-radio>
          </el-radio-group>
        </el-form-item>

        <template v-if="newUnit.primaryFlag === 2">
          <el-form-item style="font-weight: bold" label="主单位">
            <el-select v-model="newUnit.primaryId" style="width: 150px">
              <el-option v-for="primary in primaryUnit" :value="primary.primaryId" :label="primary.primaryName"/>
            </el-select>
          </el-form-item>

          <el-row style="font-weight: bold">
            <el-col>
              <el-form-item label="与主单位换算比例">
                <el-input-number style="width: 220px" min="0" v-model="newUnit.conversionScale"/>
              </el-form-item>
            </el-col>
          </el-row>
        </template>

        <el-form-item style="font-weight: bold" label="是否启用">
          <el-radio-group v-model="newUnit.unitStatus">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="2">否</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item style="font-weight: bold" label="备注">
          <el-input v-model="newUnit.unitRemark" type="textarea"/>
        </el-form-item>

      </el-form>
    </template>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancelnewUnit">取消</el-button>
        <el-button type="primary" @click="newUnitSubmit">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import {onMounted, ref, watch} from 'vue'
import {ElMessage} from "element-plus";
import unitRequest from "@/api/unit.js";

// 申明这个组件的事件  editComplete
const emits = defineEmits(['addComplete','cancelUnitAdd'])

// 弹窗标记
let unitAddWinDialogVisible = ref(false);

let props = defineProps(["primaryUnit","isShow"]);
// 编辑的单位对象
let newUnit = ref({unitStatus:1});

/**
 * 取消新增按钮事件
 */
function cancelnewUnit(){
  newUnit = ref({unitStatus:1});
  unitAddWinDialogVisible.value = false;
  emits("cancelUnitAdd")
}

/**
 * 关闭窗体前的回调函数
 */
function handleClose(){
  cancelnewUnit();
  emits("cancelUnitAdd")
}

/**
 * 新增提交
 */
function newUnitSubmit(){
  unitRequest.save(newUnit.value,res=>{
    if(res.code === 200){
      ElMessage({
        type:"success",
        message:"新增单位成功"
      });
      // 关闭弹窗
      cancelnewUnit();
      // 触发编辑完成的事件
      emits('addComplete',res.data);
    }else if (res.data == '单位编码不能为空') {
      ElMessage({
        type:"warning",
        message:"单位编码不能为空"
      });
    } else if (res.data == '单位名称不能为空') {
      ElMessage({
        type:"warning",
        message:"单位名称不能为空"
      });
    } else if (res.data == '是否是主单位必须选其一') {
      ElMessage({
        type:"warning",
        message:"是否是主单位必须选其一"
      });
    } else if (res.data == '主单位和换算比例必须选择') {
      ElMessage({
        type:"warning",
        message:"主单位和换算比例必须选择"
      });
    } else if (res.data == '是否启用必须选其一') {
      ElMessage({
        type:"warning",
        message:"是否启用必须选其一"
      });
    } else {
      ElMessage({
        type:"warning",
        message:"新增单位失败"
      });
    }
  })
}

/**
 * 侦听器 侦听 props.isShow
 */
watch(()=>props.isShow,()=>{
  unitAddWinDialogVisible.value = props.isShow;
});


onMounted(()=>{
})
</script>
<style scoped>

</style>